package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.promo.PromoService;
import com.mall.promo.constants.PromoRetCode;
import com.mall.promo.dto.*;
import com.mall.user.annotation.Anonymous;
import com.mall.user.util.IJwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author xudengfeng
 * @date 2020/12/22 10:32
 */
@RestController
@Slf4j
public class SecKillListController {

    @Reference(timeout = 3000, check = false, retries = 0)
    PromoService promoService;

    @Reference(timeout = 3000, check = false, retries = 0)
    IJwtTokenUtils jwtTokenUtils;

    @GetMapping("/shopping/seckilllist")
    @Anonymous
    public ResponseData secKill(Integer sessionId) {

        PromoProductListRequest promoProductListRequest = new PromoProductListRequest();
        promoProductListRequest.setSessionId(sessionId);
        PromoProductListResponse promoProductListResponse = promoService.queryBySessionIdAndDate(promoProductListRequest);

        String code = promoProductListResponse.getCode();

        log.info(code);

        if (PromoRetCode.SUCCESS.getCode().equals(code)) {
            return new ResponseUtil().setData(promoProductListResponse);
        }
        return new ResponseUtil().setErrorMsg(promoProductListResponse.getMsg());
    }

    @PostMapping("/shopping/promoProductDetail")
    public ResponseData promoDetail(@RequestBody Map map) {
        PromoProductDetailRequest request = new PromoProductDetailRequest();
        request.setMap(map);
        PromoProductDetailResponse response = promoService.queryPromoProductDetail(request);
        String code = response.getCode();

        log.info(response.toString());

        log.info(code);

        if (PromoRetCode.SUCCESS.getCode().equals(code)) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PostMapping("/shopping/seckill")
    public ResponseData secKillOrder(@RequestBody OrderPromoProductRequest orderPromoProductRequest, HttpServletRequest request) {

        //从cookie获取access_token，解析获取userId
        String access_token = CookieUtil.getCookieValue(request, "access_token");
        Long uid = jwtTokenUtils.getUserInfo(access_token);

        //设置id

        orderPromoProductRequest.setUserId(uid);

        OrderPromoProductResponse response = promoService.orderPromoProduct(orderPromoProductRequest);
        String code = response.getCode();

        log.warn(code);

        if (PromoRetCode.SUCCESS.getCode().equals(code)) {
            ResponseData responseData = new ResponseUtil().setData(response);
            log.info(responseData.toString());
            return responseData;
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
